package com.lc.luzhengtang.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lc.luzhengtang.R;
import com.lc.luzhengtang.adapter.DayxiaofeiListAdapter;
import com.lc.luzhengtang.base.BaseActivity;
import com.lc.luzhengtang.bean.DayxiaoFeiBean;
import com.lc.luzhengtang.conn.ConnDayConsumption;
import com.zcx.helper.http.AsyCallBack;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/6/29.
 */

public class DayConsumptionActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {

    @BindView(R.id.img_news)
    ImageView imgNews;
    @BindView(R.id.text_normal_person)
    TextView textNormalPerson;
    @BindView(R.id.recycle_personal_list)
    RecyclerView recyclePersonalList;
    @BindView(R.id.refreshLayout)
    SwipeRefreshLayout refreshLayout;

    DayxiaofeiListAdapter consumptionListAdapter;

    List<DayxiaoFeiBean.DataBean> dataBeanList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_consumption_day);
        ButterKnife.bind(this);
        dataBeanList = new ArrayList<>();
        recyclePersonalList.setLayoutManager(new LinearLayoutManager(this));
        textNormalPerson.setText(getIntent().getStringExtra("create_time"));
        initData();
    }

    @Override
    public void onRefresh() {

        new ConnDayConsumption(getIntent().getStringExtra("create_time"), getIntent().getStringExtra("buyuser_id"), new AsyCallBack<DayxiaoFeiBean>() {

            @Override
            public void onStart(int type) throws Exception {
                super.onStart(type);
            }

            @Override
            public void onSuccess(String toast, int type, Object o, DayxiaoFeiBean dayxiaoFeiBean) throws Exception {
                super.onSuccess(toast, type, o, dayxiaoFeiBean);

                dataBeanList = dayxiaoFeiBean.getData();

                if (dataBeanList != null) {
//                    currentPage = 1;
                    consumptionListAdapter.setNewData(dataBeanList);
                }
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                setRefreshing(false);
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
                consumptionListAdapter.removeAllFooterView();
                //最后调用结束刷新的方法
                setRefreshing(false);

                consumptionListAdapter.setEnableLoadMore(false);
            }
        }


        ).execute(DayConsumptionActivity.this, false);


    }


    protected void initData() {
        recyclePersonalList.setItemAnimator(new DefaultItemAnimator());
        //recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclePersonalList.setLayoutManager(new LinearLayoutManager(this));

        consumptionListAdapter = new DayxiaofeiListAdapter(null);
        consumptionListAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);
        consumptionListAdapter.isFirstOnly(false);
//        Drawable drawable = ContextCompat.getDrawable(TeamActivityCommonListActivity.this, R.drawable.line_divider);
//        recyclerViewList.addItemDecoration(new SimpleDividerItemDecoration(TeamActivityCommonListActivity.this, drawable, 1));

        recyclePersonalList.setAdapter(consumptionListAdapter);

        refreshLayout.setColorSchemeColors(Color.RED, Color.BLUE, Color.GREEN);
        refreshLayout.setOnRefreshListener(this);


        //开启loading,获取数据
        setRefreshing(true);
        onRefresh();
    }

    public void setRefreshing(final boolean refreshing) {
        refreshLayout.post(new Runnable() {
            @Override
            public void run() {
                refreshLayout.setRefreshing(refreshing);
            }
        });
    }


    @OnClick(R.id.img_news)
    public void onViewClicked() {

        finish();
    }
}
